package cn.ym.mall.service.impl;

import cn.ym.mall.dao.DealDao;
import cn.ym.mall.dao.impl.DealDaoImpl;
import cn.ym.mall.entity.Deal;
import cn.ym.mall.service.DealService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 交易表 service 层实现类
 */
public class DealServiceImpl implements DealService {
    private DealDao dealDao = new DealDaoImpl();
    /**
     * 新增交易表
     *
     * @param deal
     * @return
     * @throws SQLException
     */
    @Override
    public boolean addDeal(Deal deal) throws SQLException {
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection(false);
            if (dealDao.addDeal(deal) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据交易id查询交易信息
     *
     * @param dealid
     * @return
     */
    @Override
    public Deal getShopTrolleyByStid(Integer dealid) throws SQLException {
        Deal deal = null;
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            deal = dealDao.getDealByStid(dealid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return deal;
    }

    /**
     * 分页查询交易
     *
     * @param pageNo   当前页
     * @param pageSize 总页数
     * @return
     */
    @Override
    public Page<Deal> getPageDeal(Integer pageNo, Integer pageSize) throws SQLException {
        Connection connection= null;
        Page<Deal> page = new Page<Deal>();
        try {
            connection = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int) dealDao.getDealCount());
            page.setList(dealDao.getPageDeal(pageNo,pageSize));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 通过商家id查询交易（分页）
     *
     * @param sid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Deal> getPageDealBySid(Integer sid, Integer pageNo, Integer pageSize) throws SQLException {
        Page<Deal> page = new Page<Deal>();
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int) dealDao.getCountBySid(sid));
            page.setList(dealDao.getPageDealBySid(sid, pageNo, pageSize));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }
}
